package org.example.hypersonaltools.scene;

import javafx.collections.FXCollections;
import javafx.collections.ObservableFloatArray;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.*;
import org.example.hypersonaltools.tool.FileTools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class BooksReadToolScene implements FXScene {
    private final Scene scene;
    private final VBox root;
    private Button addBackground;
    private File selectedFile;
    private File selectedImage;
    private Button addTxt;
    private final FileTools fileTools=new FileTools();
    private TextField fontSizeInput;
    private TextField fontColorInput;
    private int fontSize;
    private String fontColor;
    private final ComboBox<String> comboBox = new ComboBox<>();;
    private String chapter;
    private ObservableList<String> chapterList;
    private List<String> chapters=new ArrayList<>();
    private final ComboBox<Float> speedComboBox= new ComboBox<>();
    private Float speed;
    private Button start;
    private ColorPicker colorPicker;

    public BooksReadToolScene() {
        root = new VBox();
        scene = new Scene(root, 800, 600);
        //
        initializeUI();
        setupEventListeners();
    }

    private void setupEventListeners() {
        addBackground.setOnAction(e -> {
            // 拉起系统文件夹选择图片
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择背景图片");
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.jpeg", "*.gif", "*.bmp"));

            // Assuming you have a reference to the Stage
            Stage primaryStage = (Stage) scene.getWindow(); // This line assumes the scene is already set on a stage
            selectedImage = fileChooser.showOpenDialog(primaryStage);

            if (selectedImage != null) {
                // You can now use the selectedFile to load and set the background image
                System.out.println("Selected file: " + selectedImage.getName());
                // Add code here to load and set the background image using the selectedFile
            }
        });

        addTxt.setOnAction(e -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("添加文档");
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("Text Files", "*.txt", "*.txt"));
            // Assuming you have a reference to the Stage
            Stage primaryStage = (Stage) scene.getWindow(); // This line assumes the scene is already set on a stage
            selectedFile = fileChooser.showOpenDialog(primaryStage);
            if (selectedFile != null) {
                //添加文档——>提取章节信息
                chapters = fileTools.getChapterMessage(selectedFile);
                List<String> list = chapters.stream().map(s -> s.substring(0, s.contains("\n")?s.indexOf("\n"):s.length()-1)).toList();
                chapterList = FXCollections.observableList(list);
                System.out.println("章节总数："+chapters.size());
//                System.out.println("Selected file: " + selectedFile.getName());
            }
        });
        fontSizeInput.textProperty().addListener((observable, oldValue, newValue) -> {
            this.fontSize=Integer.parseInt(newValue);
        });
        fontColorInput.textProperty().addListener((observable, oldValue, newValue) -> {
            this.fontColor=newValue;
        });
        comboBox.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == null&&oldValue.isEmpty()) {
                comboBox.setItems(chapterList);
            }else {
                FilteredList<String> filtered = chapterList.filtered(item -> item.contains(newValue));
                comboBox.setItems(filtered);
            }
            this.chapter=newValue;
            comboBox.show();
        });
        speedComboBox.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
            this.speed=Float.parseFloat(newValue);
        });

        start.setOnAction(e->{
            Stage stage = new Stage();
            stage.initStyle(StageStyle.TRANSPARENT);
//            stage.setAlwaysOnTop(true);
            stage.setScene(new ReadingBookScene(new Pane(),stage,selectedImage,chapters,comboBox.valueProperty().getValue(),speedComboBox.valueProperty().getValue(),this.fontSize,this.fontColor));
            stage.show();
        });

        colorPicker.setOnAction(e -> {
            Color selectedColor = colorPicker.getValue();
            fontColorInput.setText(selectedColor.toString());
        });
    }

    private void initializeUI() {
        //设置弹窗背景图片
        addBackground = new Button("添加背景图片");
        root.getChildren().add(addBackground);
        addTxt = new Button("添加文档");
        root.getChildren().add(addTxt);
        //两个输入框，设置字体大小，颜色
        fontSizeInput = new TextField();
        fontSizeInput.setPromptText("设置字体大小");
        root.getChildren().add(fontSizeInput);
        colorPicker = new ColorPicker(Color.RED);
        root.getChildren().add(colorPicker);
        fontColorInput = new TextField();
        fontColorInput.setPromptText("设置字体颜色");
        root.getChildren().add(fontColorInput);
        //设置文档显示的开始章节
        comboBox.setEditable(true); // 允许输入
        root.getChildren().add(comboBox);
        //设置自动显示速度
        speedComboBox.setItems(FXCollections.observableArrayList(0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f));
        root.getChildren().add(speedComboBox);
        //开始按钮——>带着设置启动新的窗口
        start = new Button("启动");
        root.getChildren().add(start);
    }

    @Override
    public Scene getScene() {
        return scene;
    }

    @Override
    public void onEnter() {

    }

    @Override
    public void onExit() {

    }
}
